package com.lanswon.qzsmk.controller.appManageController;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lanswon.qzsmk.business.impl.EPLoad;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.model.BikeSection;
import com.lanswon.qzsmk.service.cardManageService.QueryCardInfoService;
import com.lanswon.qzsmk.service.preService.BikeService;
import com.lanswon.qzsmk.util.CommonUtil;
import com.lanswon.qzsmk.vo.BussinessVo;
import com.lanswon.qzsmk.vo.RechargeCancelVo;
import com.lanswon.qzsmk.vo.VoucherVo;
import com.lanswon.smk.common.kmsserror.KmssException;

/**
 * 业务controller类
 * @author guyawei
 *
 */
@Controller
@RequestMapping(value="/business")
public class BusinessController  {

	@Autowired 
	EPLoad epLoad;
	
	@Autowired
	QueryCardInfoService qcs;
	
	@Autowired
	BikeService bikeService;
	
	/**
	 * 链接充值页面
	 * @return
	 */
	@RequiresPermissions("business:goDeposit")
	@RequestMapping(value="/goDeposit",name="充值页面,business:goDeposit",method=RequestMethod.GET)
	public String goDeposit(){
		
		return "Deposit";
	}
	
	/**
	 * 圈存请求(初始化)
	 * @param parm
	 * @return
	 * @throws Exception 
	 * @throws KmssException 
	 */
	@RequiresPermissions("business:loadRequest")
	@RequestMapping(value="/loadRequest",name="圈存请求,business:loadRequest",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object>loadRequest(String parm,BussinessVo bv) throws Exception, KmssException{
		System.out.println(parm);
		String[] array=parm.split(",");	
		System.out.println(new BigDecimal(CommonUtil.hexMoneyTOdouble(array[1]))+"===========================>>>");
		bv.setCrdBalBef(new BigDecimal(CommonUtil.hexMoneyTOdouble(array[0])));//交易前金额
		bv.setTxnAmt(new BigDecimal(CommonUtil.hexMoneyTOdouble(array[1])));//交易金额
		bv.setCrdBalBefHex(array[0]);//交易前金额(16进制)
		bv.setTxnAmtHex(array[1]);//交易金额(16进制)
		bv.setCrdTxnType(array[2]);//交易类型
		bv.setSamNo(array[3]);//终端机编号
		bv.setRan(array[4]);//随机数
		bv.setCurrCount(array[5]);//交易序号
		bv.setRandom(array[6]);//随机数补位
		bv.setMac1(array[7]);//mac1
		bv.setDivFactor(array[8]);//分散因子
		bv.setTxnDt(array[9]);//交易时间
		bv.setCitizenCardNo(array[10]);//市民卡号
		bv.setMicrochipNo(array[11]);//芯片号
		SecurityUtils.getSubject().getSession().setAttribute("crdBalBef", bv);//在圈存时使用
		Map<String, Object> map=epLoad.loadRequest(bv);
		return map;
	}
	
	/**
	 * 圈存
	 * @param parm
	 * @return
	 * @throws KmssException 
	 * @throws Exception 
	 */
	@RequiresPermissions("business:load")
	@RequestMapping(value="/load",name="圈存,business:load",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object>load(String param,BussinessVo bv,HttpServletRequest request) throws Exception, KmssException{
		System.out.println(param);
		String[] strs=param.split(",");
		bv.setCrdBalAft(new BigDecimal(CommonUtil.hexMoneyTOdouble(strs[6])));//交易后金额10进制
		bv.setTxnAmt(new BigDecimal(CommonUtil.hexMoneyTOdouble(strs[0])));//交易金额
		bv.setTxnAmtHex(strs[0]);//交易金额(16进制)
		bv.setCrdTxnType(strs[1]);//交易类型
		bv.setSamNo(strs[2]);//终端机编号
		bv.setCurrCount(strs[3]);//交易序号
		bv.setDivFactor(strs[4]);//分散因子
		bv.setTxnDt(strs[5]);//交易时间
		bv.setCrdBalAftHex(strs[6]);//交易后金额
		bv.setTxnTac(strs[7]);
		BussinessVo bv2=(BussinessVo) SecurityUtils.getSubject().getSession().getAttribute("crdBalBef");
		SecurityUtils.getSubject().getSession().removeAttribute("crdBalBef");
		bv.setCrdBalBef(bv2.getCrdBalBef());//交易前余额
		Map<String, Object> map=epLoad.load(bv);
		VoucherVo voucherVo=(VoucherVo) map.get("VoucherVo");
		map.put("crdBalAft", bv.getCrdBalAft());
		request.getSession().setAttribute("type", "EPPay");
		request.getSession().setAttribute("vo", voucherVo);
		return map;
	}
	
	/**
	 * 读卡显示信息
	 * @param cardNo
	 * @return
	 */
	@RequiresPermissions("business:queryInfoByReadCard")
	@RequestMapping(value="/queryInfoByReadCard",name="读卡显示信息,business:queryInfoByReadCard",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryInfoByReadCard(String citizenCardNo){
		Map<String, Object> map=qcs.queryinfoByCitizenCardNo(citizenCardNo);
		return map;
	}
	
	/**
	 * 充值撤销
	 * @param result
	 * @return
	 * @throws KmssException 
	 * @throws Exception 
	 */
	@RequiresPermissions("business:rechargeCancel")
	@RequestMapping(value="/rechargeCancel",name="充值撤销,business:rechargeCancel",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> rechargeCancel(String result,RechargeCancelVo rechargeCancelVo) throws Exception, KmssException{
		System.out.println(result);
		String [] strs=result.split(",");
		for(int i=0;i<strs.length;i++){
			if(i==9){
				System.out.println(new BigDecimal(CommonUtil.hexMoneyTOdouble(strs[i])));
				System.out.println(new BigDecimal(CommonUtil.hexMoneyTOdouble(strs[i+1])));
			}	
		}
		rechargeCancelVo.setTxnDt(strs[0]);
		rechargeCancelVo.setSamNo(strs[1]);
		if(rechargeCancelVo.getAppFlag().equals("9A00")){//9A00需要将1级和2级分散因子对调一下
			StringBuilder sb=new StringBuilder(strs[2].substring(16,32));
			String a=strs[2].substring(0,16);
			sb.append(a);
			System.out.println(sb.toString());
			rechargeCancelVo.setDivFactor(sb.toString());
		}else{
			rechargeCancelVo.setDivFactor(strs[2]);
		}
		
		rechargeCancelVo.setCrdTxnType(strs[3]);
		rechargeCancelVo.setVer(strs[4]);
		rechargeCancelVo.setTxnMac1(strs[5]);
		rechargeCancelVo.setTxnMac2(strs[6]);
		rechargeCancelVo.setTxnTac(strs[7]);
		rechargeCancelVo.setSamCurrCount(strs[8]);//16进制，清结算不需要转10进制
		rechargeCancelVo.setTxnAmtHex(strs[9]);
		rechargeCancelVo.setCrdBalAftHex(strs[10]);
		rechargeCancelVo.setTxnAmt(new BigDecimal(CommonUtil.hexMoneyTOdouble(strs[9])));
		rechargeCancelVo.setCrdBalAft(new BigDecimal(CommonUtil.hexMoneyTOdouble(strs[10])));
		rechargeCancelVo.setTradeSN(CommonUtil.patchHexString(Integer.valueOf(strs[11], 16).toString(), 4));
		rechargeCancelVo.setCrdBalBefHex(strs[12]);
		rechargeCancelVo.setCrdBalBef(new BigDecimal(CommonUtil.hexMoneyTOdouble(strs[12])));
		System.out.println(rechargeCancelVo);
		Map<String, Object> map=epLoad.checkTacByRechargeCancel(rechargeCancelVo);
		return map;
	}
	
	@RequiresPermissions("business:getBikeBlance")
	@ResponseBody
	@RequestMapping(value="/getBikeBlance",name="获取自行车账户余额,business:getBikeBlance",method=RequestMethod.POST)
	public JSONObject getBikeBlance(String citizenCardNo) throws ApplicationException{
		JSONObject jsonObject=bikeService.queryBikeUserByCitizenCardNo(citizenCardNo);
		return jsonObject;
	}
	
	@RequiresPermissions("business:bikeDeposit")
	@ResponseBody
	@RequestMapping(value="/bikeDeposit",name="自行车账户充值,business:bikeDeposit",method=RequestMethod.POST)
	public Map<String, Object> bikeDeposit(String citizenCardNo,String recharge,String vipno,String vipcardsn,String balance) throws ApplicationException{
		VoucherVo vv=bikeService.bikeAppRecharge(citizenCardNo, recharge, vipno, vipcardsn, balance);
		Map<String, Object>map=new HashMap<>();
		map.put("statusCode", "200");
		map.put("message", "充值成功");
		return map;
	}
	
	@ResponseBody
	@RequiresPermissions("business:bikeRechargeCancel")
	@RequestMapping(value="/bikeRechargeCancel",name="自行车充值撤销,business:bikeDeposit",method=RequestMethod.POST)
	public Map<String, Object> bikeRechargeCancel(String citizenCardNo) throws ApplicationException{
		Map<String, Object>map=new HashMap<>();
		bikeService.bikeAppRechargeRevoke(citizenCardNo);
		map.put("statusCode", "200");
		map.put("message", "撤销成功");
		return map;
	}
}
